{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The JOIN operation\n",
    "\n",
    "![rel](https://sqlzoo.net/w/images/c/ce/FootballERD.png)\n",
    "\n",
    "## JOIN and UEFA EURO 2012\n",
    "\n",
    "This tutorial introduces `JOIN` which allows you to use data from two or more tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.\n",
    "\n",
    "The data is available (mysql format) at <http://sqlzoo.net/euro2012.sql>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ····\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "import psycopg2\n",
    "from sqlalchemy import create_engine\n",
    "import pandas as pd\n",
    "pwd = getpass.getpass()\n",
    "engine = create_engine(\n",
    "    'postgresql+psycopg2://postgres:%s@localhost/sqlzoo' % (pwd))\n",
    "pd.set_option('display.max_rows', 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "The first example shows the goal scored by a player with the last name 'Bender'. The `*` says to list all the columns in the table - a shorter way of saying `matchid, teamid, player, gtime`\n",
    "\n",
    "**Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: `teamid = 'GER'`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "game = pd.read_sql_table('game', engine)\n",
    "goal = pd.read_sql_table('goal', engine)\n",
    "eteam = pd.read_sql_table('eteam', engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>matchid</th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1008</td>\n",
       "      <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1010</td>\n",
       "      <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1010</td>\n",
       "      <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1012</td>\n",
       "      <td>Lukas Podolski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1012</td>\n",
       "      <td>Lars Bender</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>1026</td>\n",
       "      <td>Philipp Lahm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1026</td>\n",
       "      <td>Sami Khedira</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1026</td>\n",
       "      <td>Miroslav Klose</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1026</td>\n",
       "      <td>Marco Reus</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>1030</td>\n",
       "      <td>Mesut Özil</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    matchid          player\n",
       "15     1008     Mario Gómez\n",
       "21     1010     Mario Gómez\n",
       "22     1010     Mario Gómez\n",
       "27     1012  Lukas Podolski\n",
       "29     1012     Lars Bender\n",
       "61     1026    Philipp Lahm\n",
       "63     1026    Sami Khedira\n",
       "64     1026  Miroslav Klose\n",
       "65     1026      Marco Reus\n",
       "71     1030      Mesut Özil"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goal.loc[goal['teamid']=='GER', ['matchid', 'player']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "\n",
    "From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.\n",
    "\n",
    "Notice in the that the column `matchid `in the `goal` table corresponds to the `id` column in the `game` table. We can look up information about game 1012 by finding that row in the `game` table.\n",
    "\n",
    "**Show id, stadium, team1, team2 for just game 1012**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>stadium</th>\n",
       "      <th>team1</th>\n",
       "      <th>team2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1012</td>\n",
       "      <td>Arena Lviv</td>\n",
       "      <td>DEN</td>\n",
       "      <td>GER</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      id     stadium team1 team2\n",
       "11  1012  Arena Lviv   DEN   GER"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game.loc[game['id']==1012, ['id', 'stadium', 'team1', 'team2']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "You can combine the two steps into a single query with a JOIN.\n",
    "\n",
    "```sql\n",
    "SELECT *\n",
    "  FROM game JOIN goal ON (id=matchid)\n",
    "```\n",
    "\n",
    "The **FROM** clause says to merge data from the goal table with that from the game table. The **ON** says how to figure out which rows in **game** go with which rows in **goal** - the **matchid** from **goal** must match **id** from **game**. (If we wanted to be more clear/specific we could say\n",
    "`ON (game.id=goal.matchid)`\n",
    "\n",
    "The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.\n",
    "\n",
    "**Modify it to show the player, teamid, stadium and mdate for every German goal.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>player</th>\n",
       "      <th>teamid</th>\n",
       "      <th>stadium</th>\n",
       "      <th>mdate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Mario Gómez</td>\n",
       "      <td>GER</td>\n",
       "      <td>Arena Lviv</td>\n",
       "      <td>9 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Mario Gómez</td>\n",
       "      <td>GER</td>\n",
       "      <td>Metalist Stadium</td>\n",
       "      <td>13 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Mario Gómez</td>\n",
       "      <td>GER</td>\n",
       "      <td>Metalist Stadium</td>\n",
       "      <td>13 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Lukas Podolski</td>\n",
       "      <td>GER</td>\n",
       "      <td>Arena Lviv</td>\n",
       "      <td>17 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Lars Bender</td>\n",
       "      <td>GER</td>\n",
       "      <td>Arena Lviv</td>\n",
       "      <td>17 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>Philipp Lahm</td>\n",
       "      <td>GER</td>\n",
       "      <td>PGE Arena Gdansk</td>\n",
       "      <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>Sami Khedira</td>\n",
       "      <td>GER</td>\n",
       "      <td>PGE Arena Gdansk</td>\n",
       "      <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>Miroslav Klose</td>\n",
       "      <td>GER</td>\n",
       "      <td>PGE Arena Gdansk</td>\n",
       "      <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>Marco Reus</td>\n",
       "      <td>GER</td>\n",
       "      <td>PGE Arena Gdansk</td>\n",
       "      <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>Mesut Özil</td>\n",
       "      <td>GER</td>\n",
       "      <td>National Stadium, Warsaw</td>\n",
       "      <td>28 June 2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            player teamid                   stadium         mdate\n",
       "15     Mario Gómez    GER                Arena Lviv   9 June 2012\n",
       "21     Mario Gómez    GER          Metalist Stadium  13 June 2012\n",
       "22     Mario Gómez    GER          Metalist Stadium  13 June 2012\n",
       "27  Lukas Podolski    GER                Arena Lviv  17 June 2012\n",
       "29     Lars Bender    GER                Arena Lviv  17 June 2012\n",
       "61    Philipp Lahm    GER          PGE Arena Gdansk  22 June 2012\n",
       "63    Sami Khedira    GER          PGE Arena Gdansk  22 June 2012\n",
       "64  Miroslav Klose    GER          PGE Arena Gdansk  22 June 2012\n",
       "65      Marco Reus    GER          PGE Arena Gdansk  22 June 2012\n",
       "71      Mesut Özil    GER  National Stadium, Warsaw  28 June 2012"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(game.merge(goal, left_on='id', right_on='matchid')\n",
    "    .loc[goal['teamid']=='GER', \n",
    "         ['player', 'teamid', 'stadium', 'mdate']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "Use the same `JOIN` as in the previous question.\n",
    "\n",
    "**Show the team1, team2 and player for every goal scored by a player called Mario `player LIKE 'Mario%'`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>team1</th>\n",
       "      <th>team2</th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>GER</td>\n",
       "      <td>POR</td>\n",
       "      <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>NED</td>\n",
       "      <td>GER</td>\n",
       "      <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>NED</td>\n",
       "      <td>GER</td>\n",
       "      <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>IRL</td>\n",
       "      <td>CRO</td>\n",
       "      <td>Mario Mandžukic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>IRL</td>\n",
       "      <td>CRO</td>\n",
       "      <td>Mario Mandžukic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>ITA</td>\n",
       "      <td>CRO</td>\n",
       "      <td>Mario Mandžukic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>ITA</td>\n",
       "      <td>IRL</td>\n",
       "      <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>GER</td>\n",
       "      <td>ITA</td>\n",
       "      <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>GER</td>\n",
       "      <td>ITA</td>\n",
       "      <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   team1 team2           player\n",
       "15   GER   POR      Mario Gómez\n",
       "21   NED   GER      Mario Gómez\n",
       "22   NED   GER      Mario Gómez\n",
       "32   IRL   CRO  Mario Mandžukic\n",
       "35   IRL   CRO  Mario Mandžukic\n",
       "37   ITA   CRO  Mario Mandžukic\n",
       "44   ITA   IRL  Mario Balotelli\n",
       "69   GER   ITA  Mario Balotelli\n",
       "70   GER   ITA  Mario Balotelli"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(game.merge(goal, left_on='id', right_on='matchid')\n",
    "    .loc[goal['player'].str.startswith('Mario'),\n",
    "         ['team1', 'team2', 'player']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "\n",
    "The table `eteam` gives details of every national team including the coach. You can `JOIN` `goal` to `eteam` using the phrase goal `JOIN eteam on teamid=id`\n",
    "\n",
    "**Show `player, teamid, coach, gtime` for all goals scored in the first 10 minutes `gtime<=10`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>player</th>\n",
       "      <th>teamid</th>\n",
       "      <th>coach</th>\n",
       "      <th>gtime</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Alan Dzagoev</td>\n",
       "      <td>RUS</td>\n",
       "      <td>Dick Advocaat</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Roman Shirokov</td>\n",
       "      <td>RUS</td>\n",
       "      <td>Dick Advocaat</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Silvestre Varela</td>\n",
       "      <td>POR</td>\n",
       "      <td>Paulo Bento</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Antonio Di Natale</td>\n",
       "      <td>ITA</td>\n",
       "      <td>Cesare Prandelli</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               player teamid             coach  gtime\n",
       "7        Alan Dzagoev    RUS     Dick Advocaat     15\n",
       "8      Roman Shirokov    RUS     Dick Advocaat     24\n",
       "32   Silvestre Varela    POR       Paulo Bento     87\n",
       "38  Antonio Di Natale    ITA  Cesare Prandelli     61"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(goal.merge(eteam, left_on='teamid', right_on='id')\n",
    "    .loc[goal.gtime<=10, ['player', 'teamid', 'coach', 'gtime']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.\n",
    "\n",
    "To `JOIN` `game` with `eteam` you could use either\n",
    "`game JOIN eteam ON (team1=eteam.id)` or `game JOIN eteam ON (team2=eteam.id)`\n",
    "\n",
    "Notice that because `id` is a column name in both `game` and `eteam` you must specify `eteam.id` instead of just `id`\n",
    "\n",
    "**List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mdate</th>\n",
       "      <th>teamname</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12 June 2012</td>\n",
       "      <td>Greece</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16 June 2012</td>\n",
       "      <td>Greece</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          mdate teamname\n",
       "3  12 June 2012   Greece\n",
       "4  16 June 2012   Greece"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = game.merge(eteam, left_on='team1', right_on='id')\n",
    "a.loc[a['coach']=='Fernando Santos', ['mdate', 'teamname']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.\n",
    "\n",
    "**List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Robert Lewandowski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Dimitris Salpingidis</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Alan Dzagoev</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Jakub Blaszczykowski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Giorgos Karagounis</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>Cristiano Ronaldo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>Mesut Özil</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  player\n",
       "0     Robert Lewandowski\n",
       "1   Dimitris Salpingidis\n",
       "10          Alan Dzagoev\n",
       "11  Jakub Blaszczykowski\n",
       "13    Giorgos Karagounis\n",
       "60     Cristiano Ronaldo\n",
       "69       Mario Balotelli\n",
       "71            Mesut Özil"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = goal.merge(game, left_on='matchid', right_on='id')\n",
    "a.loc[a['stadium']=='National Stadium, Warsaw', ['player']].drop_duplicates()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. More difficult questions\n",
    "\n",
    "The example query shows all goals scored in the Germany-Greece quarterfinal.\n",
    "**Instead show the name of all players who scored a goal against Germany.**\n",
    "\n",
    "> __HINT__   \n",
    "> Select goals scored only by non-German players in matches where GER was the id of either **team1** or **team2**.\n",
    "> You can use `teamid!='GER'` to prevent listing German players.\n",
    "> You can use `DISTINCT` to stop players being listed twice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Robin van Persie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Michael Krohn-Dehli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>Georgios Samaras</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>Dimitris Salpingidis</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  player\n",
       "23      Robin van Persie\n",
       "28   Michael Krohn-Dehli\n",
       "62      Georgios Samaras\n",
       "66  Dimitris Salpingidis\n",
       "69       Mario Balotelli"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = game.merge(goal, left_on='id', right_on='matchid')\n",
    "a.loc[((a['team1']=='GER') | (a['team2']=='GER')) & (a['teamid']!='GER'), \n",
    "      ['player']].drop_duplicates()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.\n",
    "Show teamname and the total number of goals scored.\n",
    "\n",
    "> __COUNT and GROUP BY__  \n",
    "> You should COUNT(*) in the SELECT line and GROUP BY teamname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>teamname</th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Croatia</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Czech Republic</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>England</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>France</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Germany</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Greece</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Italy</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Netherlands</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Poland</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Portugal</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Republic of Ireland</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Russia</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Spain</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Sweden</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Ukraine</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               teamname  player\n",
       "0               Croatia       4\n",
       "1        Czech Republic       4\n",
       "2               Denmark       4\n",
       "3               England       5\n",
       "4                France       3\n",
       "5               Germany      10\n",
       "6                Greece       5\n",
       "7                 Italy       6\n",
       "8           Netherlands       2\n",
       "9                Poland       2\n",
       "10             Portugal       6\n",
       "11  Republic of Ireland       1\n",
       "12               Russia       5\n",
       "13                Spain      12\n",
       "14               Sweden       5\n",
       "15              Ukraine       2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = eteam.merge(goal, left_on='id', right_on='teamid')\n",
    "a[['teamname', 'player']].groupby('teamname').count().reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.\n",
    "\n",
    "**Show the stadium and the number of goals scored in each stadium.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>stadium</th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Arena Lviv</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Donbass Arena</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Metalist Stadium</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>National Stadium, Warsaw</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Olimpiyskiy National Sports Complex</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>PGE Arena Gdansk</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Stadion Miejski (Poznan)</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Stadion Miejski (Wroclaw)</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               stadium  player\n",
       "0                           Arena Lviv       9\n",
       "1                        Donbass Arena       7\n",
       "2                     Metalist Stadium       7\n",
       "3             National Stadium, Warsaw       9\n",
       "4  Olimpiyskiy National Sports Complex      14\n",
       "5                     PGE Arena Gdansk      13\n",
       "6             Stadion Miejski (Poznan)       8\n",
       "7            Stadion Miejski (Wroclaw)       9"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = game.merge(goal, left_on='id', right_on='matchid')\n",
    "a[['stadium', 'player']].groupby('stadium').count().reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.\n",
    "**For every match involving 'POL', show the matchid, date and the number of goals scored.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>matchid</th>\n",
       "      <th>mdate</th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1001</td>\n",
       "      <td>8 June 2012</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1004</td>\n",
       "      <td>12 June 2012</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1005</td>\n",
       "      <td>16 June 2012</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   matchid         mdate  player\n",
       "0     1001   8 June 2012       2\n",
       "1     1004  12 June 2012       2\n",
       "2     1005  16 June 2012       1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = game.merge(goal, left_on='id', right_on='matchid')\n",
    "a.loc[(a['team1']=='POL') | (a['team2']=='POL'), \n",
    "  ['matchid', 'mdate', 'player']].groupby(['matchid', 'mdate']).count().reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.\n",
    "**For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>matchid</th>\n",
       "      <th>mdate</th>\n",
       "      <th>player</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1008</td>\n",
       "      <td>9 June 2012</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1010</td>\n",
       "      <td>13 June 2012</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1012</td>\n",
       "      <td>17 June 2012</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1026</td>\n",
       "      <td>22 June 2012</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1030</td>\n",
       "      <td>28 June 2012</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   matchid         mdate  player\n",
       "0     1008   9 June 2012       1\n",
       "1     1010  13 June 2012       2\n",
       "2     1012  17 June 2012       2\n",
       "3     1026  22 June 2012       4\n",
       "4     1030  28 June 2012       1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = game.merge(goal, left_on='id', right_on='matchid')\n",
    "a.loc[a['teamid']=='GER', \n",
    "      ['matchid', 'mdate', 'player']].groupby(['matchid', 'mdate']).count().reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.\n",
    "List every match with the goals scored by each team as shown. This will use \"CASE WHEN\" which has not been explained in any previous exercises.\n",
    "\n",
    "no  | mdate      | team1  | score1 | team2 | score2\n",
    "----|------------|--------|--------|-------|--------\n",
    "1   | July 2012  | ESP    | 4      | ITA   | 0\n",
    "10  | June 2012  | ESP    | 1      | ITA   | 1\n",
    "10  | June 2012  | IRL    | 1      | CRO   | 3\n",
    "... | ...        | ...    | ...    | ...   | ...\n",
    "\n",
    "Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. **Sort your result by mdate, matchid, team1 and team2.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mdate</th>\n",
       "      <th>team1</th>\n",
       "      <th>score1</th>\n",
       "      <th>team2</th>\n",
       "      <th>score2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>1 July 2012</td>\n",
       "      <td>ESP</td>\n",
       "      <td>4.0</td>\n",
       "      <td>ITA</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>10 June 2012</td>\n",
       "      <td>IRL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>CRO</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>10 June 2012</td>\n",
       "      <td>ESP</td>\n",
       "      <td>1.0</td>\n",
       "      <td>ITA</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>11 June 2012</td>\n",
       "      <td>UKR</td>\n",
       "      <td>2.0</td>\n",
       "      <td>SWE</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>11 June 2012</td>\n",
       "      <td>FRA</td>\n",
       "      <td>1.0</td>\n",
       "      <td>ENG</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12 June 2012</td>\n",
       "      <td>GRE</td>\n",
       "      <td>1.0</td>\n",
       "      <td>CZE</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12 June 2012</td>\n",
       "      <td>POL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>RUS</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>13 June 2012</td>\n",
       "      <td>DEN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>POR</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>13 June 2012</td>\n",
       "      <td>NED</td>\n",
       "      <td>1.0</td>\n",
       "      <td>GER</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>14 June 2012</td>\n",
       "      <td>ITA</td>\n",
       "      <td>1.0</td>\n",
       "      <td>CRO</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>14 June 2012</td>\n",
       "      <td>ESP</td>\n",
       "      <td>4.0</td>\n",
       "      <td>IRL</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>15 June 2012</td>\n",
       "      <td>UKR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>FRA</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>15 June 2012</td>\n",
       "      <td>SWE</td>\n",
       "      <td>2.0</td>\n",
       "      <td>ENG</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>16 June 2012</td>\n",
       "      <td>GRE</td>\n",
       "      <td>1.0</td>\n",
       "      <td>RUS</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16 June 2012</td>\n",
       "      <td>CZE</td>\n",
       "      <td>1.0</td>\n",
       "      <td>POL</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>17 June 2012</td>\n",
       "      <td>POR</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NED</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>17 June 2012</td>\n",
       "      <td>DEN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>GER</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18 June 2012</td>\n",
       "      <td>ITA</td>\n",
       "      <td>2.0</td>\n",
       "      <td>IRL</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>18 June 2012</td>\n",
       "      <td>CRO</td>\n",
       "      <td>0.0</td>\n",
       "      <td>ESP</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>19 June 2012</td>\n",
       "      <td>ENG</td>\n",
       "      <td>1.0</td>\n",
       "      <td>UKR</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>19 June 2012</td>\n",
       "      <td>SWE</td>\n",
       "      <td>2.0</td>\n",
       "      <td>FRA</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>21 June 2012</td>\n",
       "      <td>CZE</td>\n",
       "      <td>0.0</td>\n",
       "      <td>POR</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>22 June 2012</td>\n",
       "      <td>GER</td>\n",
       "      <td>4.0</td>\n",
       "      <td>GRE</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>23 June 2012</td>\n",
       "      <td>ESP</td>\n",
       "      <td>2.0</td>\n",
       "      <td>FRA</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>24 June 2012</td>\n",
       "      <td>ENG</td>\n",
       "      <td>0.0</td>\n",
       "      <td>ITA</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>27 June 2012</td>\n",
       "      <td>POR</td>\n",
       "      <td>0.0</td>\n",
       "      <td>ESP</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>28 June 2012</td>\n",
       "      <td>GER</td>\n",
       "      <td>1.0</td>\n",
       "      <td>ITA</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8 June 2012</td>\n",
       "      <td>RUS</td>\n",
       "      <td>4.0</td>\n",
       "      <td>CZE</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8 June 2012</td>\n",
       "      <td>POL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>GRE</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>9 June 2012</td>\n",
       "      <td>NED</td>\n",
       "      <td>0.0</td>\n",
       "      <td>DEN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>9 June 2012</td>\n",
       "      <td>GER</td>\n",
       "      <td>1.0</td>\n",
       "      <td>POR</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           mdate team1  score1 team2  score2\n",
       "30   1 July 2012   ESP     4.0   ITA     0.0\n",
       "13  10 June 2012   IRL     1.0   CRO     3.0\n",
       "12  10 June 2012   ESP     1.0   ITA     1.0\n",
       "19  11 June 2012   UKR     2.0   SWE     1.0\n",
       "18  11 June 2012   FRA     1.0   ENG     1.0\n",
       "2   12 June 2012   GRE     1.0   CZE     2.0\n",
       "3   12 June 2012   POL     1.0   RUS     1.0\n",
       "8   13 June 2012   DEN     2.0   POR     3.0\n",
       "9   13 June 2012   NED     1.0   GER     2.0\n",
       "14  14 June 2012   ITA     1.0   CRO     1.0\n",
       "15  14 June 2012   ESP     4.0   IRL     0.0\n",
       "20  15 June 2012   UKR     0.0   FRA     2.0\n",
       "21  15 June 2012   SWE     2.0   ENG     3.0\n",
       "5   16 June 2012   GRE     1.0   RUS     0.0\n",
       "4   16 June 2012   CZE     1.0   POL     0.0\n",
       "10  17 June 2012   POR     2.0   NED     1.0\n",
       "11  17 June 2012   DEN     1.0   GER     2.0\n",
       "17  18 June 2012   ITA     2.0   IRL     0.0\n",
       "16  18 June 2012   CRO     0.0   ESP     1.0\n",
       "22  19 June 2012   ENG     1.0   UKR     0.0\n",
       "23  19 June 2012   SWE     2.0   FRA     0.0\n",
       "24  21 June 2012   CZE     0.0   POR     1.0\n",
       "25  22 June 2012   GER     4.0   GRE     2.0\n",
       "26  23 June 2012   ESP     2.0   FRA     0.0\n",
       "27  24 June 2012   ENG     0.0   ITA     0.0\n",
       "28  27 June 2012   POR     0.0   ESP     0.0\n",
       "29  28 June 2012   GER     1.0   ITA     2.0\n",
       "1    8 June 2012   RUS     4.0   CZE     1.0\n",
       "0    8 June 2012   POL     1.0   GRE     1.0\n",
       "6    9 June 2012   NED     0.0   DEN     1.0\n",
       "7    9 June 2012   GER     1.0   POR     0.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = (goal[['matchid', 'teamid', 'player']]\n",
    "     .groupby(['matchid', 'teamid'])\n",
    "     .count().reset_index())\n",
    "\n",
    "(game.merge(s, how='left', left_on=['id', 'team1'], right_on=['matchid', 'teamid'])\n",
    "    .merge(s, how='left', left_on=['id', 'team2'], right_on=['matchid', 'teamid'])\n",
    "    .loc[:, ['mdate', 'team1', 'player_x', 'team2', 'player_y']]\n",
    "    .fillna(0)\n",
    "    .rename(columns={'player_x': 'score1', 'player_y': 'score2'})\n",
    "    .sort_values('mdate'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
